package cn.explink.dop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.vo.OrderVO;
import cn.explink.dop.service.OrderService;
import cn.explink.eap.web.cgform.controller.autolist.CgAutoListController;

/**
 *
 * @author songkaojun
 */
@Controller
@RequestMapping("/orderQueryController")
public class OrderQueryController extends CgAutoListController {

	@Autowired
	private OrderService orderService;

	/**
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "index")
	public ModelAndView storefrontIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dop/order/orderquery");
		return view;
	}

	/**
	 * 获取数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "loadData")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<OrderVO> loadData(int pageNum, int pageSize, HttpServletRequest request) {
		try {
			Map<String, Object> map = this.orderService.getListDataByPage(new OrderVO(), "", pageNum, pageSize);
			return (List<OrderVO>) map.get("list");
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<OrderVO>();
		}
	}

	/**
	 *
	 * @param orderVO
	 * @param pageNum
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "query")
	@ResponseBody
	public Map<String, Object> queryData(OrderVO orderVO, String deliveryManName, int pageNum, int pageSize, HttpServletRequest request) {
		try {
			return this.orderService.getListDataByPage(orderVO, deliveryManName, pageNum, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap<String, Object>();
		}
	}

	/**
	 *
	 * @param status
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getOrderListByStatus")
	@ResponseBody
	public Map<String, Object> getOrderListByStatus(@RequestParam("statusList[]") List<Integer> statusList, int pageNum, int pageSize, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if ((null == statusList) || (statusList.size() == 0)) {
				return resultMap;
			}

			// TODO 分页 不过3秒刷新一次，估计分页用处不大
			Integer count = this.orderService.getCountByStatus(null, statusList);

			// 根据总数判断当前的页数
			int maxPageNumber = (int) Math.ceil((double) count / (double) pageSize); // 可能的最大页数
			if (pageNum > maxPageNumber) {
				pageNum = maxPageNumber;
			} else if ((pageNum < 1) && (count != 0)) {
				pageNum = 1;
			}

			List<OrderVO> orderVOList = this.orderService.getOrderVOListByStatusAndDate(null, statusList, null, null, pageNum, pageSize);

			resultMap.put("count", count);
			resultMap.put("list", orderVOList);
			resultMap.put("pageNumber", pageNum);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
}
